package com.yxq.lawyermanager.dongao.controller;

import com.yxq.lawyermanager.dongao.entity.Lawyer;
import com.yxq.lawyermanager.dongao.service.ILawyerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author yxq
 * @version 1.0
 * @classname LawyerController
 * @date 2023/1/2 10:56
 */
@Controller
public class LawyerController {
    @Autowired
    ILawyerService lawyerService;

    @RequestMapping("/modify")
    public String modifyLawyer(Lawyer lawyer) {
        lawyerService.modifyLawyer(lawyer);
//        return "../static/success";
        // 跳到另一个控制器上
        return "redirect:/show";
    }

    @RequestMapping("/getLawyer")
    public String getLawyerById(Model model, int lawyerid) {
        Lawyer lawyer = lawyerService.findLawyerById(lawyerid);
        // 把查询得到的 lawyer 对象保存起来传递到网页上用
        model.addAttribute("lawyer", lawyer);
        return "../static/modifyLawyer";
    }

    @RequestMapping("/delLawyer")
    public String delLawyer(int lawyerid) {
        lawyerService.removeLawyer(lawyerid);
        return "../static/success";
    }

    @RequestMapping("/show")
    public String showLawyer(Model model) {
        List<Lawyer> lawyerList = lawyerService.findLawyer();
        // 把 lawyerList 传递到网页上
        model.addAttribute("lawList", lawyerList);
        // displayLawyer 代表网页 displayLawyer.html
        return "../static/displayLawyer";
    }

    @RequestMapping("/addLawyer")
    public String addLawyer(Lawyer lawyer) {
        System.out.println("收到请求~~~~~");
        int res = lawyerService.addLawyer(lawyer);
        return "../static/success";
    }
}
